﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Structs;
using System.IO;
using System.Diagnostics;
using MemoryController;

namespace Analyzer
{
    public class SentencesReader
    {
        private static string syntacticpath = string.Empty;
        private static List<Word> words = new List<Word>();
        public static string SetSyntacticPath
        {
            set { syntacticpath = value; }
        }


        public static void Folders(string path)
        {
            MemoryWriter.CreateDirs(path);
        }
        public static Sentence ReadSentence(string message)
        {
            Sentence sentence = new Sentence();
            int i = 0;
            while (i < message.Length)
            {
                if (message[i] == '!' || message[i] == '?' || message[i] == '.')
                {
                    switch (message[i])
                    {
                        case '!':
                            {
                                sentence.Type = '!';
                            }
                            break;
                        case '?':
                            {
                                sentence.Type = '?';
                            }
                            break;
                        case '.':
                            {
                                sentence.Type = '.';
                            }
                            break;
                    }
                    sentence.Content = message.Substring(0, i);
                    while (i < message.Length //убираем лишние знаки в конце
                            && (message[i] == '.'
                                || message[i] == '!'
                                || message[i] == '?'
                                || message[i] == '\n'
                                || message[i] == '\r')
                           )
                        i++;
                    message.Remove(0, i);
                    return sentence;
                }
                else i++;
            }
            return null;
        }
        public static string RemoveSentenceFromMessage(string sentence, string message)
        {
            string result = string.Empty;
            int i = sentence.Length;
            while (i < message.Length //убираем лишние знаки в конце
                            && (message[i] == '.'
                                || message[i] == '!'
                                || message[i] == '?'
                                || message[i] == '\n'
                                || message[i] == '\r'
                                || message[i] == ' ')
                           )
                        i++;
            result = message.Remove(0, i);
            return result;
        }
        public static void Activate(Sentence sentence, string path)
        {
            //создаем временный текстовый файл
            FileStream fs = new FileStream(path + "tmp.txt", FileMode.OpenOrCreate);
            StreamWriter sw = new StreamWriter(fs, Encoding.GetEncoding("Windows-1251"));
            sw.WriteLine(sentence.Content);
            sw.Close();

            //запускаем библиотеку Dwarf
            ProcessStartInfo process_start_info = new ProcessStartInfo(syntacticpath);
            process_start_info.Arguments = "i:" + path + "tmp.txt o:" + path + "tmp.xml xml";
            process_start_info.WorkingDirectory = path;
            process_start_info.CreateNoWindow = false;
            process_start_info.WindowStyle = ProcessWindowStyle.Hidden;

            Process.Start(process_start_info);
            System.Threading.Thread.Sleep(1000);

            //чистим после себя
            File.Delete(path + "tmp.txt");
            words = XMLparser.Read(path + "tmp.xml");
            File.Delete(path + "tmp.xml");

            MemoryWriter.RememberWords(words);
        }
    }
}
